package org.jeecg.modules.monitoring.j_monitoring.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xkcoding.http.util.StringUtil;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.config.sign.util.NoSslHttpUtils;
import org.jeecg.config.vo.HttpResult;
import org.jeecg.modules.monitoring.j_monitoring.entity.GatewayWarningCount;
import org.jeecg.modules.monitoring.j_monitoring.entity.RingDevices;
import org.jeecg.modules.monitoring.j_monitoring.entity.RingMonitor;
import org.jeecg.modules.monitoring.j_monitoring.entity.donghuan.*;
import org.jeecg.modules.monitoring.j_monitoring.facilities.FacilitiesToken;
import org.jeecg.modules.monitoring.j_monitoring.mapper.RingMonitorMapper;
import org.jeecg.modules.monitoring.j_monitoring.service.IRingMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.ResourceUtils;

import jakarta.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: ring_monitor
 * @Author: xiaoyq
 * @Date:   2023-07-12
 * @Version: V1.0
 */
@Service
public class RingMonitorServiceImpl extends ServiceImpl<RingMonitorMapper, RingMonitor> implements IRingMonitorService {

    @Autowired
    private Environment env;

    @Resource
    private RingDevicesServiceImpl ringDevicesService;

    @Resource
    private FacilitiesToken facilitiesToken;

    /**
     * 空调实时数据
     * @param name
     * @return
     */
    public ConditioningData getConditioningData(String name){
        ConditioningData data = new ConditioningData();

        String id = ringDevicesService.queryIdByName(name);
        if ("".equals(id)){
            System.out.println("未找到监控id");
            return data;
        }

        List<RingMonitor> monitorList = this.selectListByParentId(id);
        if (monitorList.size()==0){
            System.out.println("未找到监控点列表");
            return data;
        }


        try {
            List<String> stringList = new ArrayList<>();
            stringList.add(id);
            String json = facilitiesToken.getOnlineData(stringList);

            realDataRequest request = JSON.parseObject(json, realDataRequest.class);
            List<realPoints> pointsList = request.getData().getPoints();
            if (pointsList.size()==0){
                return data;
            }
            for (RingMonitor monitor : monitorList){

                if ("机组开关机状态".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    if ("1".equals(value)){
                        data.setIsOpen("开机");
                    }else {
                        data.setIsOpen("关机");
                    }
                }

                if ("空调开关机状态".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    if ("0".equals(value)){
                        data.setIsOpen("关机");
                    }else {
                        data.setIsOpen("开机");
                    }
                }

                if ("制冷模式".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    System.out.println("制冷模式"+value);
                    if ("1".equals(value)){
                        data.setIsCold("是");
                    }else {
                        data.setIsCold("否");
                    }
                }

                if ("压缩机1运行".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    if ("1".equals(value)){
                        data.setYasuo1Open("运行");
                    }else {
                        data.setYasuo1Open("空闲");
                    }
                }

                if ("压缩机1输出开关状态".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    if ("1".equals(value)){
                        data.setYasuo1Open("运行");
                    }else {
                        data.setYasuo1Open("空闲");
                    }
                }


                if ("压缩机2运行".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    if ("1".equals(value)){
                        data.setYasuo2Open("运行");
                    }else {
                        data.setYasuo2Open("空闲");
                    }
                }

                if ("压缩机2输出开关状态".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    if ("1".equals(value)){
                        data.setYasuo2Open("运行");
                    }else {
                        data.setYasuo2Open("空闲");
                    }
                }

                if ("风机运行".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    if ("1".equals(value)){
                        data.setFanOpen("运行");
                    }else {
                        data.setFanOpen("停止");
                    }
                }

                if ("风机输出开关状态".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    if ("1".equals(value)){
                        data.setFanOpen("运行");
                    }else {
                        data.setFanOpen("停止");
                    }
                }

                if ("加湿器运行".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    if ("1".equals(value)){
                        data.setHumidifierOpen("是");
                    }else {
                        data.setHumidifierOpen("否");
                    }
                }

                if ("加湿器输出开关状态".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    if ("1".equals(value)){
                        data.setHumidifierOpen("是");
                    }else {
                        data.setHumidifierOpen("否");
                    }
                }

                if ("回风温度".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    data.setTemperatureNow(value + monitor.getUnit());
                }

                if ("室内温度".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    data.setTemperatureNow(value + monitor.getUnit());
                }

                if ("温度设定值（夜晚）".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    data.setTemperatureSet(value + monitor.getUnit());
                }


                if ("回风湿度".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    data.setHumidityNow(value + monitor.getUnit());
                }

                if ("室内湿度".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    data.setHumidityNow(value + monitor.getUnit());
                }

                if ("湿度设定值".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    data.setHumiditySet(value + monitor.getUnit());
                }




            }



        }catch (Exception e){
            e.printStackTrace();
            return data;
        }



        return data;
    }


    /**
     * ups
     * @param name
     * @return
     */
    public UpsData getUpsData(String name){
        UpsData data = new UpsData();

        String id = ringDevicesService.queryIdByName(name);
        if ("".equals(id)){
            System.out.println("未找到监控id");
            return data;
        }

        List<RingMonitor> monitorList = this.selectListByParentId(id);
        if (monitorList.size()==0){
            System.out.println("未找到监控点列表");
            return data;
        }


        try {
            List<String> stringList = new ArrayList<>();
            stringList.add(id);
            String json = facilitiesToken.getOnlineData(stringList);
            realDataRequest request = JSON.parseObject(json, realDataRequest.class);
            List<realPoints> pointsList = request.getData().getPoints();
            if (pointsList.size()==0){
                return data;
            }
            for (RingMonitor monitor : monitorList){

                if ("开机/关机".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    if ("225".equals(value)){
                        data.setIsOpen("开机");
                    }else {
                        data.setIsOpen("关机");
                    }
                }

                if ("供电方式".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    if ("1".equals(value)){
                        data.setPowerSupply("UPS供电");
                    }else {
                        data.setPowerSupply("均不供电");
                    }
                }


                if ("交流输入线电压AB".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    data.setLineVoltageAB(value + monitor.getUnit());
                }

                if ("交流输入线电压BC".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    data.setLineVoltageBC(value + monitor.getUnit());
                }

                if ("交流输入线电压CA".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    data.setLineVoltageCA(value + monitor.getUnit());
                }

                if ("交流输出相电压A".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    data.setTermVoltageA(value + monitor.getUnit());
                }

                if ("交流输出相电压B".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    data.setTermVoltageB(value + monitor.getUnit());
                }

                if ("交流输出相电压C".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    data.setTermVoltageC(value + monitor.getUnit());
                }

                if ("交流输出电流A".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    data.setOutputCurrentA(value + monitor.getUnit());
                }

                if ("交流输出电流B".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    data.setOutputCurrentB(value + monitor.getUnit());
                }

                if ("交流输出电流C".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    data.setOutputCurrentC(value + monitor.getUnit());
                }

                if ("电池后备时间".equals(monitor.getName())){
                    String value = this.getDataValue(monitor.getResourceId(),pointsList);
                    data.setBatteryTime(value + monitor.getUnit());
                }

            }



        }catch (Exception e){
            e.printStackTrace();
            return data;
        }



        return data;
    }


    private List<RingMonitor> selectListByParentId(String id){
        RingMonitor monitor = new RingMonitor();
        monitor.setParentId(id);
        QueryWrapper<RingMonitor> queryWrapper = QueryGenerator.initQueryWrapper(monitor, new HashMap<>());
        return baseMapper.selectList(queryWrapper);
    }


    public String getDataValue(String id, List<realPoints> pointsList){
        for (realPoints points : pointsList){
            if (id.equals(points.getResourceId())){
                return points.getRealValue();
            }
        }
        return "";
    }

    public GatewayWarningCount getWarningCount(String name){
        GatewayWarningCount gatewayWarningCount = new GatewayWarningCount();
        try {

        }catch (Exception e){

        }


        return gatewayWarningCount;
    }


    public List<RingMonitor> getRingMonitorByIds(List<String> stringList){
        return baseMapper.selectByParentIds(stringList);
    }
}
